package com.wtgroup.demo.leetcode.q003_重复字符的最长子串;

import com.wtgroup.demo.common.util.RandomUtil;

/**
 * @author dafei
 * @version 0.1
 * @date 2021/4/24 16:10
 */
public class Q003_Test {
    public static void main(String[] args) {
        // String s = "cdd";
        // int res = new Solution2().lengthOfLongestSubstring(s);
        // System.out.println(res);

        // String[] ss = {"", " ", "abcabcbb", "bbbbb", "pwwkew", "au", "cdd", "aab"};
        String[] ss = RandomUtil.randomArrayString(1000, 200, 1234);
        Solution2 exe = new Solution2();
        for (String s : ss) {
            int res = exe.lengthOfLongestSubstring(s);
            int res1 = lengthOfLongestSubstring_1(s);
            if (res!=res1) {
                System.out.println("xxxxxxxxxxxxxxx");
                System.out.println(s);
                System.out.println(res);
                System.out.println(res1);
                break;
            }
        }
        System.out.println("!!!!!!!!!!!");
        /**
         * 0
         * 1
         * 3
         * 1
         * 3
         * 2
         */
    }

    /**2021年4月24日
     *
     * 简约: 10分; 效率: 10分; 逻辑: 6分;
     * */
    public static int lengthOfLongestSubstring_1(String s) {
        int n = s.length();
        int i=0, j=-1;
        int maxNum = 0;
        int[] table = new int[128];
        while(++j < n) {
            char cur = s.charAt(j);
            if(table[cur]>i) {
                i = table[cur];
            } else {
                maxNum = Math.max(maxNum, j-i+1);
            }
            // 新增或更新当前字符的索引位置
            table[cur] = j+1; // 默认值0, 为了错开; 取出的时候, 记得-1
        }

        return maxNum;
    }

}
